//
//  SYFenShuView.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2019/4/25.
//  Copyright © 2019年 SanYi. All rights reserved.
//

#import "SYFenShuView.h"
#import "SYFenShuCollectionViewCell.h"


@interface SYFenShuView ()<UICollectionViewDelegateFlowLayout,UICollectionViewDataSource,UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *curCollectionView;
@property (weak, nonatomic) IBOutlet UIScrollView *curScrollView;//标签
@property (weak, nonatomic) IBOutlet UIView *contView;//标签view
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contWidthLayout;
@property (assign, nonatomic) NSInteger selectNumber;
@property (assign, nonatomic) NSInteger zongscore;//总分
@property (weak, nonatomic) IBOutlet SYButton *chexiaoBtn;
@property (copy, nonatomic) NSString *reasonIds;//标签id
@end


@implementation SYFenShuView

- (void)awakeFromNib {
    [super awakeFromNib];
    //自动网格布局
    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [self.curCollectionView setCollectionViewLayout:flowLayout];
    self.curCollectionView.delegate = self;
    self.curCollectionView.dataSource = self;
    self.curCollectionView.scrollEnabled = NO;
    [self.curCollectionView registerNib:[UINib nibWithNibName:@"SYFenShuCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"SYFenShuCollectionViewCell"];
    self.selectNumber = -1;
}

- (void)setIsDetials:(BOOL)isDetials {
    _isDetials = isDetials;
    self.chexiaoBtn.hidden = NO;
}

- (void)setYjstModel:(SYYueJuanShiTiModel *)yjstModel {
    _yjstModel = yjstModel;
    if (yjstModel.isPiGai == YES) {//批改过了本地又得分。未批改没得分，服务器得分错误
        self.selectNumber = [yjstModel.score integerValue];
    } else {
        //先注销了。等服务器改过了，就改过来
        if (self.isDetials == YES) {
            self.selectNumber = yjstModel.score.length > 0?[yjstModel.score integerValue] : 1000000;
        } else {
            self.selectNumber = 100000;
        }
    }
    
    
    self.zongscore = [_yjstModel.questionScore integerValue];//总分
    if (yjstModel.correctPicture.length > 0) {
        self.selectNumber = [yjstModel.score integerValue];
    }
    
    
    [self.curCollectionView reloadData];
    
    NSArray *biaoqianArr = yjstModel.dataWrongReasonsArr;
    _contWidthLayout.constant = biaoqianArr.count *100+biaoqianArr.count*20;
    
    [self.contView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    
    UIButton *contBtn = nil;
    for (NSInteger i = 0; i < biaoqianArr.count; i ++) {
        SYAlterViewModel *alterModel = biaoqianArr[i];
        UIButton *selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [selectBtn setImage:[UIImage imageNamed:@"yj_daipigai_bxdaan"] forState:UIControlStateNormal];
        [selectBtn setImage:[UIImage imageNamed:@"yj_daipigai_xdaan"] forState:UIControlStateSelected];
        [selectBtn setTitle:SYStringWithPickFormat(@" ", alterModel.name) forState:UIControlStateNormal];
        [selectBtn setTitle:SYStringWithPickFormat(@" ", alterModel.name) forState:UIControlStateSelected];
        [selectBtn setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
        [selectBtn setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateSelected];
        [selectBtn addTarget:self action:@selector(selectBtnClick:)
            forControlEvents:UIControlEventTouchUpInside];
        selectBtn.tag = i;
        [self.contView addSubview:selectBtn];
        if ([alterModel.isCheck isEqualToString:@"1"]) {
            selectBtn.selected = YES;
        }
        
        if (i == 0) {
            [selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.left.bottom.equalTo(self.contView);
            }];
        } else {
            [selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.bottom.equalTo(self.contView);
                make.left.equalTo(contBtn.mas_right).offset(20);
            }];
            
        }
        contBtn = selectBtn;
    }
}

- (void)selectBtnClick:(UIButton *)selectBtn {
    if (_yjstModel.isPiGai == YES) {
        [MBProgressHUD zr_showError:@"已经批改过了哦~！"];
        return;
    }
    NSInteger tag = selectBtn.tag;
    selectBtn.selected =! selectBtn.selected;
    SYAlterViewModel *alterModel = _yjstModel.dataWrongReasonsArr[tag];
    alterModel.isSelect = selectBtn.selected;
    if (alterModel.isSelect == YES) {
        alterModel.isCheck = @"1";
    } else {
        alterModel.isCheck = @"0";
    }
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [_yjstModel.questionScore integerValue]+1;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SYFenShuCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SYFenShuCollectionViewCell" forIndexPath:indexPath];
    cell.numberLab.text = [NSString stringWithFormat:@"%ld",indexPath.row];
    if (indexPath.row == self.selectNumber) {
        cell.numberLab.backgroundColor = [UIColor colorWithHexString:@"#20B097"];
        cell.numberLab.textColor = [UIColor colorWithHexString:@"#FFFFFF"];
    } else {
        cell.numberLab.textColor = TITLECOLOR;
        cell.numberLab.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF"];
    }
    return cell;
}
/**
 cell的大小
 */
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(50, 50);
}
/**
 每个分区的内边距（上左下右）
 */
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(10, 10, 10, 10);
    
}
/**
 分区内cell之间的最小行间距
 */
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}
/**
 分区内cell之间的最小列间距
 */
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}
/**
 点击了某个item
 */
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (_yjstModel.isPiGai == YES) {
        [MBProgressHUD zr_showError:@"已经批改过了哦~！"];
        return;
    }
    _selectNumber = indexPath.row;
    [self.curCollectionView reloadData];
    if (indexPath.row == 0) {//错误
        _yjstModel.fenSu = @"0";
    } else if (indexPath.row == self.zongscore) {
        _yjstModel.fenSu = @"1";
    } else {
        _yjstModel.fenSu = @"2";
    }
    
    _yjstModel.score = SJStringWithFormat(indexPath.row);
    // [self selectBtnClickType:SYFenShuViewTypeRequest];
    //    TOP
    self.reasonIds = @"";
    for (SYAlterViewModel *alterModel in _yjstModel.dataWrongReasonsArr) {
        if (alterModel.isSelect == YES) {
            self.reasonIds = [NSString stringWithFormat:@"%@,%@",self.reasonIds,alterModel.biqianid];
        }
    }
    
    if (self.isDetials == YES) { //重新批改
        [MBProgressHUD mj_showMessage:nil toView:self];
        [SYRequestAPI yj_requestCorrectServiceReCorrectQuestionCorrectId:self.correctId score:SJStringWithFormat(indexPath.row) isRight:_yjstModel.fenSu answerImagePath:@"" wrongReasonIds:self.reasonIds success:^(BOOL isSuccess) {
            [MBProgressHUD mj_hideHUDForView:self];
            [MBProgressHUD zr_showError:@"批改成功"];
        } fail:^(NSError *error) {
            [MBProgressHUD mj_hideHUDForView:self];
            [MBProgressHUD zr_showError:error.message];
        }];
        
        
        
    } else { //批改试题
        
        
        
        __weak typeof(self) weakSelf = self;
        //学生答案id传什么 批改痕迹图片路径传什么
        [MBProgressHUD mj_showMessage:nil toView:self];
        [SYRequestAPI yj_requestcorrectServiceCorrectQuestionAnswerId:_yjstModel.studentAnswerFileId isRight:_yjstModel.fenSu score:SJStringWithFormat(indexPath.row) studentAnswerFileId:_yjstModel.studentAnswerFileId answerImagePath:@"" wrongReasonIds:self.reasonIds isCorrectQuestion:_yjstModel.isPiGai success:^(BOOL isSuccess) {
            [MBProgressHUD mj_hideHUDForView:self];
            weakSelf.yjstModel.isPiGai = YES;
            [weakSelf.curCollectionView reloadData];
            [weakSelf selectBtnClickType:SYFenShuViewTypeRequest];
        } fail:^(NSError *error) {
            [MBProgressHUD mj_hideHUDForView:self];
            [MBProgressHUD zr_showError:error.message];
        }];
    }
    
    

    
    
}

- (void)selectBtnClickType:(SYFenShuViewType)type {
    if ([self.delegate respondsToSelector:@selector(selectItemBtnIndex:fenshuViewType:)]) {
        [self.delegate selectItemBtnIndex:self.selectIndex fenshuViewType:type];
    }
}
//70 上一题  71 下一题
- (IBAction)selectXuanTiBtnClick:(SYButton *)sender {
    
    NSInteger tag = sender.tag;
    SYFenShuViewType type = -1;
    switch (tag) {
        case 70:
            type = SYFenShuViewTypeShangyige;
            if (self.selectIndex == 0) {
                [MBProgressHUD zr_showError:@"这是第一题哦~！"];
                return;
            }
            self.selectIndex = self.selectIndex - 1;
            break;
        case 71:
            type = SYFenShuViewTypeXiayige;
            break;
        case 73:
            type = SYFenShuViewTypeCheXiao;
            break;
        default:
            break;
    }
    [self selectBtnClickType:type];
    
}


@end























